package mysql

import (
	"onerk4/zk2/common/global"
	"onerk4/zk2/common/model"
	"time"
)

func QueryUser(mobile string) (result *model.User, err error) {
	err = global.DB.Where("mobile = ?", mobile).Limit(1).Find(&result).Error
	return
}

func QueryCourseList() (result []*model.Course, err error) {
	err = global.DB.Where("status = ?", 1).Find(&result).Error
	return
}

func QueryCourseInfo(id int64) (result *model.Course, err error) {
	err = global.DB.Where("id = ?", id).First(&result).Error
	return
}

func QueryReservationCourse(id int64, status int64, now time.Time) (result []*model.CourseReservation, err error) {
	err = global.DB.Where("user_id = ? and status = ?", id, status).Where("created_at > ?", now).Find(&result).Error
	return
}

func QueryReservationCourseInfo(id int64, uid int64) (result *model.CourseReservation, err error) {
	err = global.DB.Where("user_id = ? and id = ?", uid, id).Limit(1).Find(&result).Error
	return
}

func DelQueryReservation(id int64, uid int64) error {
	return global.DB.Model(&model.CourseReservation{}).Where("user_id = ? and id = ?", uid, id).Limit(1).Update("status", 4).Error
}
